/*
 *   Copyright (C) 2009 Ultratronik Entwicklungs GmbH 
 *
 *     - Author: Sören Wellhöfer <soeren.wellhoefer@gmx.net>
 *
 *   This program is free software; you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation; either version 2 of the License, or
 *   (at your option) any later version.
 *
 */

#ifndef AUX_HPP
#define AUX_HPP

// todo: put includes into implementation unit!

#include <iostream>
#include <string>
#include <sstream>


template <class T> inline std::string to_string(const T& val)
{
	std::stringstream stream;
	stream << val;
	return stream.str();
}

/*
 * Interfacing with the MAIplayer.
 */
class Player {
	
public:
	enum Codec {
		MPEG4_VID = 0,
		MPEG4,
		MPEG2_VID,
		MPEG2
	};

	enum State {
		STOPPED,
		PAUSED,
		PLAYING,
		ERROR
	};

private:

	static const std::string exec_name;
	static const std::string cmd_codecs[];
	
	int fd_cmd;
	FILE *resp;

	bool initialized;
	State status;
	std::string last_err_msg;

	void tell(std::string cmd);
	
public:
	Player() : initialized(false), status(STOPPED)
	{ }

	~Player()
	{
		fclose(resp);
		close(fd_cmd);
	}

	std::string& last_err()
	{
		return last_err_msg; 
	}

	Player::State get_state()
	{
		return status;
	}
	
	inline bool init_check()
	{
		if(!initialized) {
			std::cerr << "player not initialized; forgot open()?" << std::endl;
			return true;
		}
		return false;
	}
				 
	void launch(int dispw, int disph);

	/*
	 * Checks the player status by parsing the return output from
	 * maiplayer. Should be called at frequent intervals.
	 * (By a QTimer or anything that is appropriate; threads, eg...)
	 */
	void update_status();

	void open(Player::Codec codec, std::string path);
	void play();
	void pause();
	void stop();
};

#endif
